﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace Nizm0.QOT.Library.Objects
{
    public class SearchedObject : IXmlSerializable
    {
        private Table _table;

        private Type _objectType;

        private string _path;

        private DateTime _finishedAt;

        public Table Table
        {
            get { return _table; }
            set { _table = value; }
        }

        public DateTime FinishedAt
        {
            get { return _finishedAt; }
            set { _finishedAt = value; }
        }

        public SearchedObject(Table table, string path, DateTime finished)
            : this(table, path)
        {
            _finishedAt = finished;
        }

        public SearchedObject(Table table, string path)
            : this()
        {//_objectType.FullName
            _table = table;
            _objectType = table.GetType();
            _path = path;
            if (_finishedAt != null && _finishedAt != DateTime.MinValue) { }
            else
            {
                _finishedAt = table.MaxDate;
            }

            dynamic dynobj = (dynamic)Convert.ChangeType(_table, _objectType);
        }
        public SearchedObject()
        {
        }

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            _table = Settings.Instance.ProjectSettings.FindObjectByPath(reader["Table"], reader["Path"], typeof(Table));
            if (_table == null)
                _table = new Table(reader["Table"]);
            _path = reader["Path"];
            DateTime.TryParse(reader["FinishedAt"], out _finishedAt);
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("Table", _table.Name);
            writer.WriteAttributeString("Path", _path);
            writer.WriteAttributeString("FinishedAt", _finishedAt.ToString());
        }

        public string Serialize()
        {
            using (Stream str = new MemoryStream())
            {
                //System.Runtime.Serialization.Formatters.Binary.BinaryFormatter t = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(_objectType);
                x.Serialize(str, this);
                XmlDocument xDocument = new XmlDocument();
                str.Position = 0;
                xDocument.Load(str);
                return xDocument.OuterXml;
                //using (StreamReader stmr = new StreamReader(str))
                //{
                //    return stmr.ReadToEnd();
                //}
            }
        }

        public override string ToString()
        {
            return this._path;
        }
    }
}
